package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.model.User;
import com.zr.d_c.service.UserService;
import com.zr.d_c.serviceimpl.UserServiceImpl;
import com.zr.d_c.util.MD5Util;
/**
 * 修改密码
 * @author Administrator
 *
 */
public class ModifyPswController extends HttpServlet {

	UserService userservice = new UserServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String upsw = req.getParameter("upsw");
		String newupsw = req.getParameter("newupsw");
		String renewpsw = req.getParameter("renewpsw");
		HttpSession session = req.getSession();
		User u = (User) session.getAttribute("currentUser");
		PrintWriter pw = resp.getWriter();
		if(MD5Util.MD5(upsw).equals(u.getUpsw())){
			if(newupsw.equals(renewpsw)){
				userservice.modifyUpswByUid(u.getUid(), newupsw);
				session.setAttribute("currentUser",userservice.getUserByUnameAndUpsw(u.getUname(), newupsw));
				pw.write("1");
			}else{
				pw.write("0");
			}
		}else{
			pw.write("0");
		}
		pw.close();
	}
}
